package cn.com.headfree.sysmenu.service.impl;

import cn.com.headfree.base.common.MyConstant;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.sysmenu.dao.IDeptUserDao;
import cn.com.headfree.sysmenu.service.IDeptUserService;
import cn.com.headfree.sysmenu.vo.OrgDeptVo;
import cn.com.headfree.sysmenu.vo.TreeSelect;
import com.gta.edu.sdk.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;


@Service("deptUserService")
public class DeptUserServiceImpl implements IDeptUserService {

    @Autowired
    public IDeptUserDao deptUserDao;

    /**
     * 查询单位树信息
     * @param orgDept
     * @param user
     * @return
     */
    @Override
    public List<OrgDeptVo> searchDeptList(OrgDeptVo orgDept, User user){
        //表示第一次进入
        if(StringUtil.isNullOrEmpty(orgDept.getId())){
            if(MyConstant.ROLE_LEVE_SHEN.equals(user.getRemark())){
                orgDept.setId(MyConstant.DEPT_TOP_ID);
            }else {
                if(user.getIsHeadDept() != null && user.getIsHeadDept() == 0){ //本部可以看到全公司的人
                    orgDept.setId(user.getpId());
                }else{
                    orgDept.setId(user.getBaseOrgId());
                }
            }
        }
        return deptUserDao.searchDeptList(orgDept);
    }


    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<OrgDeptVo> orgDept){
        List<OrgDeptVo> orgDeptTrees = buildMenuTree(orgDept);
        return orgDeptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param orgDept 菜单列表
     * @return 树结构列表
     */
    public List<OrgDeptVo> buildMenuTree(List<OrgDeptVo> orgDept){
        List<OrgDeptVo> returnList = new ArrayList<OrgDeptVo>();
        List<String> tempList = new ArrayList<String>();
        for (OrgDeptVo dept : orgDept)
        {
            tempList.add(dept.getId());
        }
        for (Iterator<OrgDeptVo> iterator = orgDept.iterator(); iterator.hasNext();)
        {
            OrgDeptVo menu = (OrgDeptVo) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getpId()))
            {
                recursionFn(orgDept, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = orgDept;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<OrgDeptVo> list, OrgDeptVo t)
    {
        // 得到子节点列表
        List<OrgDeptVo> childList = getChildList(list, t);
        t.setChildren(childList);
        for (OrgDeptVo tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<OrgDeptVo> getChildList(List<OrgDeptVo> list, OrgDeptVo t)
    {
        List<OrgDeptVo> tlist = new ArrayList<OrgDeptVo>();
        Iterator<OrgDeptVo> it = list.iterator();
        while (it.hasNext())
        {
            OrgDeptVo n = (OrgDeptVo) it.next();
            if (null!=n.getpId() && (n.getpId().equals(t.getId())))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<OrgDeptVo> list, OrgDeptVo t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}